package com.team.ai.service;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.team.ai.entity.Content;
import com.team.ai.entity.ResultEntity;
import com.team.ai.entity.User;
import com.team.ai.mapper.AiListMapper;
import com.team.ai.mapper.ContentMapper;
import com.team.ai.mapper.UserMapper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Component
@Transactional
@Service
public class ContentService {
    private static final Logger logger= LoggerFactory.getLogger(ContentService.class);//日志getLogger需要传入的类名
    @Resource
    ContentMapper contentMapper;
    @Resource
    JuHeService juHeService;
    @Resource
    GptService gptService;
    @Resource
    AiListMapper aiListMapper;

    @Resource
    UserMapper userMapper;



//    public String insert(String text,String daan,int userId){
//        try{
//            return ResultEntity.success(contentMapper.insert(text,daan,userId));
//        }catch (Exception e){
//            logger.error(e.getLocalizedMessage());
//            return  ResultEntity.fail(-10005);
//        }
//    }

    public int delete(int id){
        try{
            return contentMapper.delContent(id) ;
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return  -1;
        }
    }


    public Content selectByPrimaryKey(int id){
        try{
            return contentMapper.selectByPrimaryKey(id);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public String selectList(int userId,int aiId){
        try {
            return ResultEntity.success(contentMapper.selectList(userId,aiId)) ;
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10005);
        }
    }

    /**
     * 向数据库添加问题与答案
     * @param text 问题
     * @param id 提问人ID
     * @return String
     */
    public String addq(String text,int id,int aiId,String pic){
        try{
            String res=juHeService.chatgpt(text);
            if(!res.equals("")){
                User user = userMapper.selectByPrimaryKey(id);
                pic=user.getAvatar();
                 contentMapper.addq(text,res,id,aiId,pic);
                System.out.println(aiListMapper.update(res,id,aiId));
            }
            return res;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }


}
